import {defineConfig} from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path"; // 导入path模块

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [vue()],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src') // 添加@别名指向src目录
        }
    },
    server: {
        host: "0.0.0.0",
        port: 3000,
        cors: true,
        proxy: {
            "/api": {
                target: "http://localhost:8080",
                changeOrigin: true,
                secure: false,
                ws: true,
                configure: (proxy, options) => {
                    // 代理事件处理
                    proxy.on('error', (err, req, res) => {
                        console.error('代理错误:', err);
                    });
                    proxy.on('proxyReq', (proxyReq, req, res) => {
                        console.log('正在代理请求:', req.url);
                    });
                    proxy.on('proxyRes', (proxyRes, req, res) => {
                        console.log('收到代理响应:', req.url, proxyRes.statusCode);
                    });
                }
            },
            "/test": {
                target: "http://tengyuxin-bucket.oss-cn-beijing.aliyuncs.com",
                ws: true,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/test/, '')
            }
        },
    },
    // 添加构建优化选项
    build: {
        minify: 'terser',
        terserOptions: {
            compress: {
                drop_console: false, // 生产环境保留console，便于调试
                drop_debugger: true  // 移除debugger语句
            }
        },
        rollupOptions: {
            output: {
                manualChunks: {
                    // 将第三方库拆分成单独的块
                    vendor: ['vue', 'vue-router', 'element-plus', 'axios'],
                }
            }
        }
    }
});
